﻿// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

// ReSharper disable CheckNamespace
// ReSharper disable CommentTypo
// ReSharper disable IdentifierTypo
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedType.Global

/* StoreCommand.cs --
 * Ars Magna project, http://arsmagna.ru
 */

#region Using directives

using System.IO;

using AM;

#endregion

#nullable enable

namespace ManagedIrbis.Mx.Commands;

/// <summary>
///
/// </summary>
public sealed class StoreCommand
    : MxCommand
{
    #region Properties

    #endregion

    #region Construction

    /// <summary>
    /// Constructor.
    /// </summary>
    public StoreCommand()
        : base("Store")
    {
    }

    #endregion

    #region Private members

    #endregion

    #region Public methods

    #endregion

    #region MxCommand members

    /// <inheritdoc cref="MxCommand.Execute" />
    public override bool Execute
        (
            MxExecutive executive,
            MxArgument[] arguments
        )
    {
        OnBeforeExecute();

        var fileName = "output.txt";
        if (arguments.Length != 0)
        {
            fileName = arguments[0].Text;
        }

        if (!string.IsNullOrEmpty(fileName))
        {
            using var writer = File.CreateText(fileName);
            foreach (var record in executive.Records)
            {
                writer.WriteLine(record.Mfn.ToInvariantString());
            }
        }

        OnAfterExecute();

        return true;
    }

    #endregion

}
